package com.guguskins.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guguskins.back.entity.GuCommonProblem;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuCommonProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-06-02
 */
@RestController
@RequestMapping("/commonProblem")
public class GuCommonProblemController {

    @Autowired
    private GuCommonProblemService commonProblemService;

    @GetMapping("/list")
    public ResultVo getList(@RequestParam(value = "problemType", required = false, defaultValue = "0") int problemType) {
        List<GuCommonProblem> commonProblems = commonProblemService.list(new LambdaQueryWrapper<GuCommonProblem>() {
            {
                eq(problemType > 0, GuCommonProblem::getProblemType, problemType);
                orderByAsc(GuCommonProblem::getProblemSort);
            }
        });
        return ResultVo.success(commonProblems);
    }

    @GetMapping("/back/list")
    public ResultVo listPage(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize
    ) {

        return ResultVo.success(commonProblemService.pageList(pageNum, pageSize));
    }

    @PostMapping("/back/update")
    public ResultVo commonProblemUpdate(@RequestBody GuCommonProblem guCommonProblem) {
        commonProblemService.updateById(guCommonProblem);
        return ResultVo.success("修改成功");
    }

    @PostMapping("/back/insert")
    public ResultVo commonProblemInsert(@RequestBody GuCommonProblem guCommonProblem) {
        commonProblemService.save(guCommonProblem);
        return ResultVo.success("添加成功");

    }

  @GetMapping("back/findById")
    public ResultVo findById(Integer id){

        return ResultVo.success( commonProblemService.getById(id),"成功");
  }
  @GetMapping("back/remove")
    public ResultVo backRemove(@RequestParam(value = "id",required = false)Integer id){
        if(commonProblemService.removeById(id)) {

            return ResultVo.success("删除成功");
        }
        return ResultVo.fail("删除失败");
  }

}
